﻿using OF.Component.Base;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OP.manage.post
{
    public partial class postdetail : OpBasePage
    {
        public int Id
        {
            get
            {
                return EConvert.ToInt(Request.QueryString["id"]);
            }
        }

        public int Type
        {
            get
            {
                return EConvert.ToInt(Request.QueryString["type"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Type == 1)
                {
                    PostSendBind();
                }
                else
                {
                    PostReceiveBind();
                }
                GetShopImageById();
            }
        }

        private void PostSendBind()
        {
            var oInfo = PostSendOperate.GetByID(Id);
            if (oInfo != null)
            {
                this.ltLikeCount.Text = oInfo.LikeCount.ToString();
                this.ltCreatedDate.Text = oInfo.CreatedDate.ToString("yyyy-MM-dd HH:mm:ss");
                this.ltPersonPrice.Text = oInfo.PersonPrice.ToString();
                this.ltTradePrice.Text = oInfo.TradePrice.ToString();
                this.ltContent.Text = oInfo.Content;
                this.ltCommentCount.Text = oInfo.CommentCount.ToString();
                this.ltBizTypeName.Text = GetBizTypeNameById(oInfo.BizTypeID);
                SetShopInfoById(oInfo.ShopID);
                this.ltCityName.Text = GetLocationInfoById(oInfo.LocationID);
            }
        }

        private void PostReceiveBind()
        {
            var oInfo = PostReceiveOperate.GetByID(Id);
            if (oInfo != null)
            {
                this.ltLikeCount.Text = oInfo.LikeCount.ToString();
                this.ltCreatedDate.Text = oInfo.CreatedDate.ToString("yyyy-MM-dd HH:mm:ss");
                this.ltPersonPrice.Text = oInfo.PersonPrice.ToString();
                this.ltTradePrice.Text = oInfo.TradePrice.ToString();
                this.ltContent.Text = oInfo.Content;
                this.ltCommentCount.Text = oInfo.CommentCount.ToString();
                this.ltBizTypeName.Text = GetBizTypeNameById(oInfo.BizTypeID);
                SetShopInfoById(oInfo.ShopID);
                this.ltCityName.Text = GetLocationInfoById(oInfo.LocationID);
            }
        }


        protected string GetLocationInfoById(int lId)
        {
            BaseLocation_AllInfo info = BaseLocation_AllOperate.GetByID(lId);
            if (info != null)
            {
                return string.Format("{0}{1}", info.PName, info.CName);
            }
            return "";
        }

        protected void GetShopImageById()
        {
            IList<PostImgInfo> list = PostImgOperate.QueryList(a => a.PostID == Id&&a.PostType== Type);
            list.ForEach(a => {
                a.ImgUrl = AppSettings.UploadFileFullHttpDir + a.ImgUrl;
            });
            this.rptDataList.DataSource = list;
            this.rptDataList.DataBind();
        }

        protected void SetShopInfoById(int id)
        {
            ShopInfo info = ShopOperate.GetByID(id);
            if (info != null)
            {
                this.ltShopName.Text = info.ShopName;
                this.ltUserName.Text = info.UserName;
                this.ltGender.Text = info.Gender == 1 ? "男" : "女";
                this.ltMobile.Text = info.Mobile;
            }
           
        }

        private string GetBizTypeNameById(int id)
        {
            string name = "";
            BizTypeInfo info = BizTypeOperate.QuerySingle(a => a.BizTypeID == id);
            if (info!=null)
            {
                name = info.BizTypeName;
            }
            return name;

        }
    }
}